const app = getApp() // 引入app
// 根据路由显示不同的加载文字
let num = 0

const setTitle = (url) => {
    let title = '加载中'
    switch (url) {
        case '/user/login':
            title = '登陆中'
            break;
        case '/user/loginWXAdds':
            title = '登陆中'
            break;
        default:
            break;
    }
    return title
}

// 防止重复弹出Modal
const onLogin = () => {
    wx.showModal({
        title: '您还未登录',
        content: '请先登录再进行操作',
        success(res) {
            if (res.confirm) {
                wx.reLaunch({
                    url: '/pages/login/index',
                })
                num = 0
            } else if (res.cancel) {
                num = 0
            }
        }
    })
}

// 信息认证
const onAuth = () => {
    wx.showModal({
        title: '您还未进行信息认证',
        content: '为了完整的进行体验，请先完成信息认证再进行操作。',
        cancelText: '暂不认证',
        confirmText: '立即认证',
        success(res) {
            if (res.confirm) {
                wx.reLaunch({
                    url: '/pages/auth/index',
                })
                num = 0
            } else if (res.cancel) {
                num = 0
            }
        }
    })
}

const retard = () => {
    onLogin()
        ++num
}

const request = (url, options) => {
    return new Promise((resolve, reject) => {
        // 监听网络状态，失败则跳转网络失败页面
        wx.onNetworkStatusChange(function (res) {
            if (res.isConnected == false && res.networkType == "none") {
                wx.navigateTo({
                    url: '/pages/networkanomaly/index',
                })
            }
        })
        // 加载状态文字
        wx.showLoading({
            title: setTitle(url),
        })
        var requesturl = app.globalData.baseurl;
        wx.request({
            url: `${requesturl}${url}`,
            method: options.method,
            data: options.data,
            header: {
                'content-type': options.isObj ? 'application/json' : 'application/x-www-form-urlencoded',
              'token': wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
            },
            success(request) {
                resolve(request)
                if (request.data.code !== 200) {
                    wx.showToast({
                        title: request.data.message,
                        icon: 'none',
                        duration: 1000
                    })
                    if (request.data.code === 300 && num == 0) {
                        retard()
                    }
                } else {
                    // wx.showToast({
                    //     title: request.data.message,
                    //     icon: 'none',   
                    //     duration: 1000   
                    // })
                }
            },
            fail(error) {
                console.log(error, 'error');
                reject(error.data)
            },
            complete: () => {
                setTimeout(() => {
                    wx.hideLoading();
                }, 100);
            }
        })
    })
}

const get = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    }
    if (!options) {
        options = {}
    }
    return request(url, {
        method: 'GET',
        data: options
    })
}

//post对象
const postObj = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    }
    return request(url, {
        method: 'POST',
        data: options,
        isObj: true
    })
}

//post参数
const post = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    }
    return request(url, {
        method: 'POST',
        data: options,
        isObj: false
    })
}

const put = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    }
    return request(url, {
        method: 'PUT',
        data: options
    })
}

// 不能声明DELETE（关键字）
const remove = (url, options, type) => {
    if (!type) {
        url = app.globalData.storePrefix + url
    } else if (type === app.globalData.mallType) {
        url = app.globalData.mallPrefix + url
    } else if (type === app.globalData.msgType) {
        url = app.globalData.msgPrefix + url
    } else if (type === app.globalData.userType) {
        url = app.globalData.userPrefix + url
    }
    return request(url, {
        method: 'DELETE',
        data: options
    })
}

module.exports = {
    get,
    post,
    put,
    remove,
    postObj,
}